package com.itboye.hutouben.activity.login;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;

import com.itboye.hutouben.base.BaseActivity;
import com.itboye.hutouben.base.MyApplcation;
import com.itboye.hutouben.presenter.UserPresenter;
import com.itboye.hutouben.util.ByAlert;
import com.itboye.hutouben.util.Const;
import com.itboye.hutouben.util.SPUtils;
import com.itboye.hutouben.volley.ResultEntity;

import java.util.Observable;
import java.util.Observer;

public class MyService extends Service implements Observer {
    String uid;
    UserPresenter userPresenter;
    public static final String EVENT_TYPE_UNKNOWN = BaseActivity.class
            .getName() + "_unknown";

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        userPresenter = new UserPresenter(this);

    }

    final Handler handler = new Handler();
    Runnable thread = new Runnable() {

        @Override
        public void run() {
            uid = (String) SPUtils.get(MyApplcation.ctx, null,
                    Const.ID, "");
            if (uid == null || "".equals(uid) || uid == "") {

            } else {
//                PersonDataBean bean = new PersonDataBean();
//                bean.setUid(uid);
//                bean.setAutoLoginCode((String) SPUtils.get(
//                        MyApplcation.ctx, null,
//                        SPContants.Auto_Code, ""));
//                userPresenter.Autologin(bean);
            }

            handler.postDelayed(this, 6000);
        }
    };

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        handler.postDelayed(thread, 6000);
        return super.onStartCommand(intent, flags, startId);

    }

    @Override
    public boolean onUnbind(Intent intent) {
        // TODO Auto-generated method stub
        return super.onUnbind(intent);
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        handler.removeCallbacks(thread);
    }

    public ResultEntity handlerError(Object data) {
        ResultEntity result = (ResultEntity) data;
        if (result == null) {
            result = new ResultEntity("-1", "数据格式错误!~", data);
            return result;
        }

        if (result.hasError()) {
           ByAlert.alert(result.getMsg());
            result.setEventType(EVENT_TYPE_UNKNOWN);
            return result;
        }

        return result;
    }

    @Override
    public void update(Observable observable, Object data) {
        // TODO Auto-generated method stub
        ResultEntity entity = handlerError(data);
        if (entity != null) {
//            if (entity.getEventType() == userPresenter.AutoLoginSuccess) {
//                AutoLoginSuccess(entity);
//            } else if (entity.getEventType() == MainPresenter.AutoLoginFail
//                    || entity.getEventType() == EVENT_TYPE_UNKNOWN) {
//                MyToast.toast(entity.getMsg() + "");
//                SPUtils.put(MainApplication.context, null,
//                        SpPublicParams.USER_NAME, "");
//                SPUtils.put(MainApplication.context, null,
//                        SpPublicParams.USERHEAD, "");
//                SPUtils.put(MainApplication.context, null,
//                        SpPublicParams.NICK_NAME, "");
//                SPUtils.put(MainApplication.context, null, SpPublicParams.UID,
//                        "");
//                SPUtils.put(MainApplication.context, null, SpPublicParams.SEX,
//                        "");
//                SPUtils.put(MainApplication.context, null,
//                        SpPublicParams.SIGNATURE, "");
//                SPUtils.put(MainApplication.context, null,
//                        SpPublicParams.PHONE_MODEL, "");
//                SPUtils.put(MainApplication.context, null, SpPublicParams.URL,
//                        "");
//                Intent intent = new Intent("newshead");
//                sendBroadcast(intent);
//                Intent intent3 = new Intent(MainApplication.context,
//                        MainActivity.class);
//                intent3.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                MainApplication.context.startActivity(intent3);
//                Intent intent2 = new Intent("gotoshopcart");
//                intent2.putExtra("index", 4);
//                sendBroadcast(intent2);
//                //
//                // delDialog = new UIAlertView(MainApplication.context, "", "",
//                // "返回", "重新登陆");
//                // delDialog.show();
//                // delDialog
//                // .setClicklistener(new UIAlertView.ClickListenerInterface() {
//                //
//                // public void doLeft() {
//                // Intent intent = new Intent(
//                // MainApplication.context,
//                // MainActivity.class);
//                // startActivity(intent);
//                // delDialog.dismiss();
//                // }
//                //
//                // public void doRight() {
//                // Intent intent = new Intent(
//                // MainApplication.context,
//                // MainActivity.class);
//                // startActivity(intent);
//                // Intent intent2 = new Intent("gotoshopcart");
//                // intent2.putExtra("index", 4);
//                // sendBroadcast(intent2);
//                // delDialog.dismiss();
//                // }
//                // });
//            }
        }
    }

//    void AutoLoginSuccess(ResultEntity entity) {
//        // LoginController.setLoginState(new LoginedState());
//        UserBean bean = (UserBean) entity.getData();
//        SPUtils.put(MainApplication.context, null, SpPublicParams.UID,
//                bean.getUid() + "");
//        SPUtils.put(MainApplication.context, null, SpPublicParams.USER_NAME,
//                bean.getUsername() + "");
//        SPUtils.put(MainApplication.context, null, SpPublicParams.PASSWORD,
//                bean.getPassword() + "");
//        SPUtils.put(MainApplication.context, null, SpPublicParams.NICK_NAME,
//                bean.getNickname() + "");
//        SPUtils.put(MainApplication.context, null, SpPublicParams.PHONE_MODEL,
//                bean.getMobile() + "");
//        SPUtils.put(MainApplication.context, null, SpPublicParams.realname,
//                bean.getRealname() + "");
//        SPUtils.put(MainApplication.context, null, SpPublicParams.USERHEAD,
//                bean.getHead());
//        SPUtils.put(MainApplication.context, null, SpPublicParams.SEX,
//                bean.getSex() + "");
//        SPUtils.put(MainApplication.context, null,
//                SpPublicParams.AcceptAddress, bean.getDefaultAddress() + "");
//        SPUtils.put(MainApplication.context, null, SpPublicParams.Company,
//                bean.getCompany() + "");
//        SPUtils.put(MainApplication.context, null, SpPublicParams.Job,
//                bean.getJobTitle() + "");
//        SPUtils.put(MainApplication.context, null,
//                SpPublicParams.AcceptAddress, bean.getLocArea() + "");
//        SPUtils.put(MainApplication.context, null, SpPublicParams.USERHEAD,
//                bean.getHead() + "");
//        SPUtils.put(MainApplication.context, null, SpPublicParams.WX,
//                bean.getWeixin() + "");
//        SPUtils.put(MainApplication.context, null, SpPublicParams.IS_LOGINED,
//                "1");
//        SPUtils.put(MainApplication.context, null, SpPublicParams.Auto_Code,
//                bean.getAutoLoginCode() + "");
//        String time = String.valueOf((int) (Math.random() * 10));
//        SPUtils.put(MainApplication.context, null, SpPublicParams.URL,
//                GlobalConfig.IMAGE_HEAD + bean.getUid() + "&&random" + time);
//    }
}
